package com.michaelbulava.TFS.ui.controls.idea;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.michaelbulava.TFS.ui.controls.generic.CheckboxTable;
import com.michaelbulava.TFS.ui.controls.generic.IProjectDependantControl;
import com.microsoft.tfs.util.Check;

import javax.swing.*;
import java.util.Comparator;

/**
 * Created by mbulava on 7/14/2015.
 */
public class ModuleCheckboxList extends CheckboxTable<Module> implements IProjectDependantControl {

    private CheckItemsStateChangedListener listener;
    private Project myProject;

    public interface CheckItemsStateChangedListener{
        void onCheckItemsStateChanged();
    }

    public void setProject(Project project){
        Check.notNull(project, "project");
        myProject = project;
        setData(ModuleManager.getInstance(myProject).getModules());
    }

    @Override
    public Project getProject() {
        return myProject;
    }

    @Override
    protected CheckboxItem<Module> createNewCheckBoxItem(Module item) {
        return new ModuleCheckBox(item);
    }

    @Override
    protected Class getClassForColumn(int columnIndex) {
        return String.class;
    }

    @Override
    protected Object getColumnValue(int columnIndex, Module item) {
        switch(columnIndex){
            case 0:
                return item.getName();
            default:
                return null;
        }
    }

    @Override
    protected String[] getColumnNames() {
        return new String[] {"Module(s)"};
    }

    @Override
    protected int getCheckboxItemColumnIndex() {
        return 0;
    }

    @Override
    protected void CheckboxItemStateChanges(CheckboxItem<Module> item){
        if (listener != null)
            listener.onCheckItemsStateChanged();
    }

    @Override
    protected Comparator<?> getSorterComparator() {
        return String.CASE_INSENSITIVE_ORDER;
    }

    public void setCheckItemsStateChangedListener(CheckItemsStateChangedListener listener){
        this.listener = listener;
    }

    protected class ModuleCheckBox extends CheckboxItem<Module>{

        public ModuleCheckBox(Module item) {
            super(item);
        }

        @Override
        protected String getTextForItem() {
            return getItem().getName();
        }

        @Override
        protected Icon getIconForItem() {
            return null;
        }
    }
}
